package com.example.forum.Controller;
/* @Author      zouzh
 * @PackageName forum
 * @Package     com.example.forum.Controller
 * @Date        2023/9/16 19:50
 */

import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.exception.ApplicationException;
import com.example.forum.model.Board;
import com.example.forum.service.IBoardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Api(tags = "板块接口")
@Slf4j
@RequestMapping("/board")
@RestController
public class BoardController {

    // 从配置文件中读取值，如果没有配置，默认值为 9
    @Value("${forum.index.board-num:9}")
    private Integer indexBoardNum;
    @Autowired
    private IBoardService boardService;


    /**
     *  查询首页板块列表
     * @return
     */
    @ApiOperation("获取首页板块列表")
    @GetMapping("/topList")
    public AppResult<List<Board>> topList(){

        log.info("首页板块个数为：" + indexBoardNum);

        List<Board> boards= boardService.selectByNum(indexBoardNum);
        // 调用 service 查询结果
        if (boards == null ){
            boards = new ArrayList<>();
        }
        // 返回结果
        return AppResult.success(boards);
    }

    @ApiOperation("获取板块详情")
    @GetMapping("/getById")
    public AppResult<Board> getBoardInfo(@ApiParam("板块 id") @RequestParam("id") @NonNull  Long id){
        Board board = boardService.selectById(id);
        if (board == null || board.getDeleteState() == 1){
            log.info(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        return AppResult.success(board);
    }
}
